package binarysearchtree;


public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    //二叉搜索树的根节点
    public TreeNode root;


    //查找
    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key) {
                cur = cur.right;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                return true;
            }
        }
        return false;
    }


    //插入，创建二叉搜索树
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        //1.按照二叉树的性质，查找到插入节点的位置
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                //在二叉搜索树中不能插入相同数据
                return false;
            }
        }
        //2.插入新节点
        TreeNode node = new TreeNode(key);
        if (parent.val > key) {
            //插入到左边
            parent.left = node;
        } else {
            //插入到右边
            parent.right = node;
        }
        return true;
    }


    //删除
    public void remove(int val) {
        //1.按照二叉树的性质，查找到删除节点的位置
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else {
                //开始删除
                removeNode(cur, parent);
            }
        }
    }

    private void removeNode(TreeNode cur, TreeNode parent) {
        //删除时 判断该节点的子树的情况
        if (cur.left == null) {
            //待删除节点左边一定是空的
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }

        } else if (cur.right == null) {
            //待删除节点右边一定是空的
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            //待删除节点左右都不为空
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            //找右树里面最小的数据[右树最左边的数据]
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            //拿到后覆盖待删除节点
            cur.val = target.val;
            //删除target，根据target节点不同情况来删除
            if (targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }
}
